﻿/*CLR Version: 4.0.30319.18063
 * Creat Date: 2015/3/13 9:42:36
 * Creat Year: 2015
 * Creator: 程炜.Snail
 */
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;


namespace Utility.Contracts
{
    /// <summary>
    /// 主键栏位名,值
    /// </summary>
    public class PKColumn
    {
        /// <summary>
        /// 栏位名称
        /// </summary>
        public string Column { get; set; }
        /// <summary>
        /// 栏位值
        /// </summary>
        public dynamic Value { get; set; }
    }
    /// <summary>
    /// 可持久化到数据库的数据模型基类
    /// </summary>
    [Serializable]
    [JsonObject(MemberSerialization.OptOut)]
    public abstract class EntityBase
    {
        /// <summary>
        /// 
        /// </summary>
        protected EntityBase()
        {
            //IsDeleted = false;
            //CreatedTime = DateTime.Now;
        }
        /// <summary>
        /// 创建新的主键
        /// </summary>
        /// <returns></returns>
        [JsonIgnore]
        public string NewKey
        {
            get
            {
                return Guid.NewGuid().ToString().ToUpper();
            }
        }
        /// <summary>
        /// 获取当前时间
        /// </summary>
        /// <returns></returns>
        [JsonIgnore]
        public DateTime NowSystemTime
        {
            get { return DateTime.Now; }
        }
        /// <summary>
        /// 主键改变
        /// </summary>
        [JsonIgnore]
        private Dictionary<string, object> _PKPropertyChanges = new Dictionary<string, object>();
        /// <summary>
        /// 栏位值改变
        /// </summary>
        [JsonIgnore]
        private List<string> _PropertyChanges = new List<string>();
        /// <summary>
        /// 修改的主键栏位
        /// </summary>
        [JsonIgnore]
        public Dictionary<string, object> PKPropertyChanges
        {
            get { return _PKPropertyChanges; }
        }
        /// <summary>
        /// 修改的栏位
        /// </summary>
        [JsonIgnore]
        public List<string> PropertyChanges
        {
            get { return _PropertyChanges; }
        }
        /// <summary>
        /// 添加主键修改项
        /// </summary>
        /// <param name="propertyName">属性名称</param>
        protected void AddPKChange(string propertyName, dynamic oldValue, dynamic newValue)
        {
            object v = oldValue;
            if (null == oldValue || string.IsNullOrEmpty(Convert.ToString(oldValue)))
            {   //如果旧的值不存在,则不记录
                v = newValue;
            }
            if (!_PKPropertyChanges.Keys.Contains(propertyName))
            {
                _PKPropertyChanges.Add(propertyName, v);
            }
            else
            {
                _PKPropertyChanges[propertyName] = v;
            }
        }
        /// <summary>
        /// 添加修改项
        /// </summary>
        /// <param name="propertyName">属性名称</param>
        protected void AddPropertyChange(string propertyName)
        {
            if (!_PropertyChanges.Contains(propertyName))
            {
                _PropertyChanges.Add(propertyName);
            }
        }
        /// <summary>
        /// 清除修改项
        /// </summary>
        public void ClearChanges()
        {
            _PropertyChanges.Clear();
            _PKPropertyChanges.Clear();
        }
    }
}
/*
忽略某些属性
类似本文开头介绍的接口优化，实体中有些属性不需要序列化返回，可以使用该特性。首先介绍Json.Net序列化的模式:OptOut 和 OptIn
OptOut  默认值,类中所有公有成员会被序列化,如果不想被序列化,可以用特性JsonIgnore
OptIn   默认情况下,所有的成员不会被序列化,类中的成员只有标有特性JsonProperty的才会被序列化,当类的成员很多,但客户端仅仅需要一部分数据时,很有用

仅需要姓名属性

    [JsonObject(MemberSerialization.OptIn)]
    public class Person
{
    public int Age { get; set; }

    [JsonProperty]
    public string Name { get; set; }

    public string Sex { get; set; }

    public bool IsMarry { get; set; }

    public DateTime Birthday { get; set; }
}
  不需要是否结婚属性

复制代码
    [JsonObject(MemberSerialization.OptOut)]
    public class Person
{
    public int Age { get; set; }

    public string Name { get; set; }

    public string Sex { get; set; }

    [JsonIgnore]
    public bool IsMarry { get; set; }

    public DateTime Birthday { get; set; }
}
*/